<template>
  <div class="app-container">
    <el-row :gutter="10" class="search-top">
      <el-col :span="1.5">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="搜索" prop="ip">
            <el-input
              v-model="queryParams.hostName"
              placeholder="请输入IP、主机名称"
              clearable
              @keyup.enter.native="handleQuery"
            >
              <el-button slot="append" icon="el-icon-search" @click="handleQuery" ></el-button>
            </el-input>
          </el-form-item>
        </el-form>
      </el-col>
      <el-col :span="1.5" style="float: right;">
        <el-button
          type="primary"
          plain
          icon="el-icon-circle-plus-outline"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['machine:machine:add']"
        >添加主机</el-button>
      </el-col>
    </el-row>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="machineList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" width="50"/>
      <el-table-column label="IP地址" align="center" width="150">
        <template slot-scope="scope">
          <el-button type="text" @click="machineDetail(scope.row)">{{scope.row.ip}}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="端口" align="center" prop="sshPort" width="50"/>
      <el-table-column label="机房" align="center" prop="idc" width="100"/>
      <el-table-column label="主机类型" align="center" prop="serverType" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.kw_machine_type" :value="scope.row.serverType"/>
        </template>
      </el-table-column>
      <el-table-column label="主机名称" align="center" prop="hostName" width="200"/>
      <el-table-column label="系统类型" align="center" prop="os" width="200"/>
      <el-table-column label="处理器" align="center" prop="cpu" />
      <el-table-column label="网卡名称" align="center" prop="netCardName" />
      <el-table-column label="添加时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <div class="badge-status-container">
            <span :class="'badge-status-do badge-status-'+scope.row.status"/>
            <dict-tag :options="dict.type.kw_host_status" :value="scope.row.status"/>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            :title="canDoTitle(scope.row)"
            :disabled="!canDoOperation(scope.row)"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['machine:machine:edit']"
          >修改</el-button>
          <el-popconfirm
            v-hasPermi="['machine:machine:remove']"
            confirm-button-text='好的'
            cancel-button-text='不用了'
            icon="el-icon-info"
            icon-color="red"
            title="该操作会将主机的所有信息和配置永久删除？"
            @confirm="handleDelete(scope.row)"
          >
            <el-button
              :title="canDoTitle(scope.row)"
              :disabled="!canDoOperation(scope.row)"
              size="mini"
              type="text"
              icon="el-icon-delete"
              slot="reference"
            >删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <add-machine v-if="open" :id="id" @closeDrawer="getList"/>
  </div>
</template>

<script>
import { listMachine,  delMachine } from "@/api/machine/machine";
import AddMachine from "@/views/machine/addMachine";
export default {
  name: "Machine",
  dicts: ['kw_machine_type','kw_host_status'],
  components: {AddMachine},
  comments:{
    AddMachine
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      id: undefined,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 主机信息表格数据
      machineList: [],
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ip: null,
        hostName: null,
        idc: null,
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询主机信息列表 */
    getList() {
      this.open = false;
      this.loading = true;
      listMachine(this.queryParams).then(response => {
        this.machineList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.id=undefined;
      this.open = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.id=row.id;
      this.open = true;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      delMachine(ids).then(res =>{
        this.getList();
        this.$modal.msgSuccess("删除主机任务创建成功");
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('machine/machine/export', {
        ...this.queryParams
      }, `machine_${new Date().getTime()}.xlsx`)
    },
    canDoOperation(row){
      if(row.status==='submit' || row.status==='deleted')
        return false;
      return true;
    },
    canDoTitle(row){
      if(!this.canDoOperation(row)){
        return "主机正在创建或正在删除中，无法操作。"
      }
      return "";
    },
    machineDetail(row){
      this.$router.push("/machine/detail/"+row.id+"/"+row.ip);
    },
  }
};
</script>
